
var addId = 0;
var lastSel = 1;


pageLoad = function(){
	setDatePicker("#fromDate");
	setDatePicker("#toDate");
	loadPlan();
}

addPro = function(part) {
	addId=addGrid(proTable,part,"partCode",addId);
};


selectOrder = function() {
	var grs = jQuery("#proTable").jqGrid('getGridParam', 'selarrrow');
	if (grs.length > 0)
		jQuery("#proTable").jqGrid('delGridRow', grs, {
			reloadAfterSubmit : false
		});
	else
		alert("请选择要删除行！");
	// 若点击了全选按钮，重置全选按钮框
	$("#proTable").jqGrid('resetSelection');
};
/**
 *
 * 选择一行数据时的处理方法。
 * 主要是根据ITEM_ID
 *
 */
onExpend = function(itemId) {
	 $("#orderItemTable").jqGrid('GridUnload');
	 $("#orderItemTable").jqGrid({
	     url:"workOrderAction!queryWorkOrderseByItemId.action?itemId="+itemId,
	     datatype:"json", 
	     mtype:"POST",//提交方式
	     height:320,//高度，表格高度。可为数值、百分比或'auto'
	     autowidth:true,//自动宽
	     colNames:['批次号','姓名','工序名称','单件工时','总工时','首检员','首检时间','数量','合格数量','检验员','收检人','收检时间','车间主任','要求完成时间','发货时间','创建时间','操作','位置'],
	     colModel:[
	         {name:'batchNum',index:'batchNum', width:'40%',align:'right'},
	         {name:'workerName',index:'workerName', width:'35%', align:"left", sortable:true},
	         /*{name:'procedure.wpCode',index:'wpCode', width:'30%', align:"left", sortable:true},**/
	         {name:'procedure.wpName',index:'wpName', width:'40%', align:"right", sortable:true},
	         {name:'itemWorkflow.manHours',index:'wpName', width:'45%', align:"right", sortable:true},
	         {
	         	name:'procedure.wpName',
	         	index:'wpName', 
	         	width:'35%', 
	         	align:"right", 
	         	sortable:true,
	         	formatter:function(value, grid, rows, state){
	         		var quantity = rows["quantity"];
	         		var manhour = rows["itemWorkflow"]["manHours"];
	         		return quantity*manhour;
	         	}
	         },
	         {name:'firstInspectionName',index:'firstInspectionName', width:'35%', align:"left", sortable:true},
	         {name:'firstInspectionTime',index:'firstInspectionTime', width:'45%', formatter:'date', formatoptions:{newformat: 'Y年m月d日'},align:"left"},
	         {name:'quantity',index:'quantity', width:'30%', align:"right", sortable:true},
	         {name:'qualifiedQuantity',index:'qualifiedQuantity', width:'25%', align:"right", sortable:true},
	         {name:'qualityInspector',index:'qualityInspector', width:'30%', align:"left", sortable:true},
	         {name:'receivingInspection',index:'receivingInspection', width:'30%', align:"left", sortable:true},
	         {name:'receivingInspectionDate',index:'receivingInspectionDate', width:'30%', formatter:'date', formatoptions:{newformat: 'Y年m月d日'},align:"left"},
	         {name:'workshopLeader',index:'workshopLeader', width:'30%', align:"left", sortable:true},
	         {name:'dueDate',index:'dueDate', width:'45%', formatter:'date', formatoptions:{newformat: 'Y年m月d日'},align:"left"},
	         {name:'deliveryDate',index:'deliveryDate',formatter:'date', formatoptions:{newformat: 'Y年m月d日'},width:'45%', align:"left"},
	         {name:'modifiedDate',index:'modifiedDate',formatter:'date', formatoptions:{newformat: 'Y年m月d日'},width:'45%', align:"left"},
	         {
				name : 'id',
				index : 'id',
				width : '35%',
				align : 'center',
				hidden: false,
				formatter: function (value, grid, rows, state) { 
					var status = rows["status"];
					var operator = "分配";
					var batchNum = rows["batchNum"];
					var returnVal = "未知操作";
					/**
					 * 如果是首道工序，且状态为NEW,则可以分配，
					 */
					if("NEW"==status){
						returnVal = "等待分配";
					}else if("ASSIGNED"==status){
						returnVal = "<a href=\"javascript:void(0);\" style=\"color:#f60\" onclick=\"doFirstCheck(" + value + ","+batchNum+")\">首检</a>";
					}else if("FCHECHED"==status || "FCHECK"==status){
						returnVal = "<a href=\"javascript:void(0);\" style=\"color:#f60\" onclick=\"doInspect(" + value + ","+batchNum+")\">收检</a>";
					}else if("INSPECTED"==status ){
						returnVal = "已完成";
					}else if("REPORTED"==status ){
						returnVal = "已上报";
					}else if("ACTIVE"==status){
                        returnVal = "<a href=\"javascript:void(0);\" style=\"color:#f60\" onclick=\"assign(" + value + ","+batchNum+")\">分配</a>";
                    }else{
                    }
					return returnVal ;
			 	}
			},
			
	        {name:'sequence',index:'sequence', align:"left",hidden:true}
	         
	     ],
	     rownumbers:true,//添加左侧行号
	     altRows:true,//设置为交替行表格,默认为false
	     //sortname:'createDate',
	     //sortorder:'asc',
	     viewrecords: true,//是否在浏览导航栏显示记录总数
	     rowNum:15,//每页显示记录数
	//     rowTotal:2000,
	     rowList:[15,20,25],//用于改变显示行数的下拉列表框的元素数组。
	     loadonce:true,
	     gridview: true,  
         caption :'生产工单状态',
	     jsonReader:{
	         root:"workOrders",
	         repeatitems : false
	     },
	     pager:$('#orderPager')
 	});
};
/**
 *分配工作单
 *
 */
assign = function(id,batchNum){
	window.location.href="workOrderAction!assignWorkOrder?id="+id+"&batchNum="+batchNum; 
	/*
	createNewFieldToForm("_form", "jsonData");
	createNewFieldToForm("_form", "batchNum");
	
	document._form.batchNum.value = batchNum;
	document._form.action = "productionAction!insertProOrder";
	document._form.method = "post";
	document._form.submit();
	**/
};

doFirstCheck = function(id,batchNum){
	window.location.href="workOrderAction!initWorkOrder?id="+id+"&batchNum="+batchNum; 
};

doInspect = function(id,batchNum){
	window.location.href="workOrderAction!initWorkOrder?id="+id+"&batchNum="+batchNum; 
};
/**
 *查询按钮的处理方法
 *
 */
loadPlan = function() {
	$("#planTable").jqGrid('GridUnload');
	$("#planTable").jqGrid({
		onSelectRow: function(id){ 
		   if(id && id!==lastSel){ 
			  jQuery('#planTable').restoreRow(lastSel); 
			  lastSel=id; 
		   } 
		   var rowData = $("#planTable").jqGrid("getRowData",id);//
		   var expendCode= rowData.itemId;
		   onExpend(expendCode);
		},
		url : "${ctx}/productionAction!listPlannedPOItems.action?selectedOrder="+$("#orderList").val(),
		datatype : "json", // 数据来源，本地数据
		mtype : "POST",// 提交方式
		height : 300,// 高度，表格高度。可为数值、百分比或'auto'
		width : 600,// 这个宽度不能为百分比
		autowidth : true,// 自动宽
		colNames : [ 'ITEM ID','订单号','物料号', '物料名称', '零件类型', '需求数量', '自制/外协' ],
		colModel : [ {
			name : 'itemId',
			index : 'itemId',
			sortable : true,
			width : 120,
			align : "center"
		},{
			name : 'orderNum',
			index : 'orderNum',
			sortable : true,
			width : 120,
			align : "center"
		},{
			name : 'part.partCode',
			index : 'partCode',
			sortable : true,
			width : 120,
			align : "center"
		}, {
			name : 'part.partName',
			index : 'partName',
			sortable : true,
			width : 120,
			align : "center"
		},{
			name : 'part.partType',
			index : 'partType',
			sortable : true,
			width : 120,
			align : "center"
		},{
			name : 'requiredQty',
			index : 'requiredQty',
			sortable : true,
			width : 120,
			align : "center"
		},{
			name : 'proType',
			index : 'proType',
			sortable : true,
			
			formatter: 'checkbox',
			width : 120,
			align : 'center',
			edittype: 'checkbox',
			editable : true,
			editoptions: {value: 'true:false', defaultValue: 'false'}
		} ],
		rownumbers : true,// 添加左侧行号
		altRows:true,//设置为交替行表格,默认为false
		// sortname:'createDate',
		// sortorder:'asc',
		viewrecords : true,// 是否在浏览导航栏显示记录总数
		rowNum : 50,// 每页显示记录数
		// rowTotal:2000,
		rowList : [ 15, 20, 25,500 ],// 用于改变显示行数的下拉列表框的元素数组。
		loadonce : true,
		//multiselect : true,
		gridview : true,
		//cellEdit : true,
		viewrecords : true,		
		// 定义了单元格内容保存位置
		cellsubmit : 'clientArray',// 单元格提交方法，数据保存在客户端。不需设置url:""
        caption :'排产计划详情',
		jsonReader : {
			root : "productionList",
			id : "itemId",
			repeatitems : false
		},
		pager : $('#planPager')
	});
};

function productionPlan(){
	var grs = jQuery("#planTable").jqGrid('getGridParam', 'selarrrow');
	var selObj = new Array();
	
	for(var i=0;i<grs.length;i++){
		selObj[i]=$("#planTable").jqGrid("getRowData", grs[i]);
	}
	//var rowData = $("#partTable").jqGrid("getRowData", grs);//

	createNewFieldToForm("data_form", "jsonData");
	createNewFieldToForm("data_form", "selectedOrder");
	
	document.data_form.jsonData.value = JSON.stringify(selObj);
	document.data_form.selectedOrder.value = $("#orderList").val();
	document.data_form.action = "productionAction!insertProOrder";
	document.data_form.method = "post";
	document.data_form.submit();

};